package org.fkit.formatter;

import org.springframework.format.Formatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by feiyu on 17-3-29.
 */
public class DateFormatter implements Formatter<Date> {

    private String datePattern;
    private SimpleDateFormat dateFormat;

    public DateFormatter(String datePattern) {
        this.datePattern = datePattern;
        this.dateFormat = new SimpleDateFormat(datePattern);
    }

    public Date parse(String s, Locale locale) throws ParseException {
        try{
            return dateFormat.parse(s);
        }catch (Exception e){
            throw new IllegalArgumentException(e);
        }

    }

    public String print(Date date, Locale locale) {
        return dateFormat.format(date);
    }
}
